package com.nebula.dao.impl;

import com.nebula.entity.User;
import com.nebula.dao.Database;
import com.nebula.dao.IUserDao;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author CcccX
 * @create 2019-01-03 16:39
 */
public class UserDaoImpl extends BaseDaoImpl<User> implements IUserDao {

    /**
     * 验证用户名是否存在
     *
     * @param username 用户名
     * @return int
     */
    @Override
    public int checkLoginUsername(String username) {
        String sql = "select count(1) from user where BINARY username = '" + username + "'";
        ResultSet rs = (ResultSet) Database.excute(sql);
        int resultCount = 0;
        try {
            if (rs != null) {
                while (rs.next()) {
                    resultCount = rs.getInt(1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultCount;
    }

    @Override
    public int login(String username, String password) {
        String sql = "select count(1) from user where BINARY username = ? and password = ?";
        ResultSet rs = (ResultSet) Database.excute(sql, new Object[]{username, password});
        int resultCount = 0;
        resultCount = BaseDaoImpl.getResultCount(resultCount, rs);
        return resultCount;
    }

    @Override
    public User findByUsername(String username) {
        User user = new User();
        String sql = "select * from user where BINARY username = ?";
        ResultSet rs = (ResultSet) Database.excute(sql, username);
        try {
            if (rs != null) {
                while (rs.next()){
                    user.setId(rs.getString("id"));
                    user.setRealname(rs.getString("realname"));
                    user.setGender(rs.getString("gender"));
                    user.setRoleId(rs.getInt("role_id"));
                    user.setUsername(rs.getString("username"));
                    user.setCreatedTime(rs.getTimestamp("created_time"));
                    user.setUpdatedTime(rs.getTimestamp("updated_time"));
                    user.setQuestionOne(rs.getInt("question_one"));
                    user.setQuestionTwo(rs.getInt("question_two"));
                    user.setQuestionThree(rs.getInt("question_three"));
                    user.setAnswerOne(rs.getString("answer_one"));
                    user.setAnswerTwo(rs.getString("answer_two"));
                    user.setAnswerThree(rs.getString("answer_three"));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        user.setUsername(username);
        return user;
    }

    /*private static User assembleUser(ResultSet rs) throws SQLException {
        User user = new User();
        user.setId(rs.getString("id"));
        user.setUsername(rs.getString("username"));
        user.setPassword(rs.getString("password"));
        user.setRealname(rs.getString("realname"));
        user.setCreatedTime(rs.getTimestamp("created_time"));
        user.setUpdatedTime(rs.getTimestamp("updated_time"));
        return user;
    }*/
}
